package com.example.onlinereadingsystem.demos.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.onlinereadingsystem.demos.web.dto.domain.*;
import com.example.onlinereadingsystem.demos.web.dto.request.NovelSearchDetailInfoParam;
import com.example.onlinereadingsystem.demos.web.dto.vo.NovelDetailInfoVo;
import com.example.onlinereadingsystem.demos.web.dto.vo.NovelSearchDetailInfoVo;
import com.example.onlinereadingsystem.demos.web.util.Result;
import com.example.onlinereadingsystem.demos.web.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @Description 首页
 * @Author ljm
 * @Date 2024/10/12
 */
@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private IndexNovelService indexNovelService;
    @Autowired
    private NovelService novelService;
    @Autowired
    private NovelKindService novelKindService;
    @Autowired
    private BannerService bannerService;
    @Autowired
    private ChapterService chapterService;

    /**
     * @auther ljm
     * @create 2024/10/12
     * @describe 获取首页推荐小说信息
     */
    @Override
    public Result<List<NovelDetailInfoVo>> getIndexInfo() {
        List<IndexNovel> list = indexNovelService.list();
        List<NovelDetailInfoVo> resList = new ArrayList<>();
        list.forEach(indexNovel -> {
            LambdaQueryWrapper<Novel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Novel::getId, indexNovel.getNovelId());
            Novel novel = novelService.getOne(queryWrapper);
            LambdaQueryWrapper<NovelKind> kindWrapper = new LambdaQueryWrapper<>();
            kindWrapper.eq(NovelKind::getId, novel.getNovelKindId());
            String kindName = novelKindService.getOne(kindWrapper).getKindName();
            NovelDetailInfoVo infoVo = new NovelDetailInfoVo();
            BeanUtils.copyProperties(novel, infoVo);
            if (novel.getIsEnd() == 1) {
                infoVo.setStatus("已完结");
            } else {
                infoVo.setStatus("连载中");
            }
            infoVo.setNovelKind(kindName);
            resList.add(infoVo);
        });
        return Result.OK(resList);
    }

    /**
     * @auther ljm
     * @create 2024/10/17
     * @describe 获取首页轮播图
     */
    @Override
    public Result<List<Banner>> getBannerList() {
        return Result.OK(bannerService.list());
    }

    /**
     * @auther ljm
     * @create 2024/11/14
     * @describe 小说搜索接口
     */
    @Override
    public Result<IPage<NovelSearchDetailInfoVo>> searchNovel(NovelSearchDetailInfoParam param) {
        LambdaQueryWrapper<Novel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Novel::getTitle, param.getKeyWord());
        Page<Novel> page = new Page<>(param.getPage(), param.getSize());
        page = novelService.page(page, queryWrapper);
        List<Novel> novelList = page.getRecords();
        List<NovelSearchDetailInfoVo> resList = new ArrayList<>();
        novelList.forEach(item -> {
            NovelSearchDetailInfoVo infoVo = new NovelSearchDetailInfoVo();
            BeanUtils.copyProperties(item, infoVo);
            infoVo.setNovelKindName(novelKindService.getById(item.getNovelKindId()).getKindName());
            List<Chapter> chapters = chapterService.getChapterListByNovelId(item.getId());
            if (!chapters.isEmpty()) {
                chapters.sort(Comparator.comparing(Chapter::getChapterNum));
                Chapter firstChapter = chapters.get(0);
                Chapter LastChapter = chapters.get(chapters.size() - 1);
                infoVo.setFirstChapterId(firstChapter.getId());
                infoVo.setLastChapterNum(LastChapter.getChapterNum());
                infoVo.setLastChapterTitle(LastChapter.getChapterName());
                infoVo.setLastChapterId(LastChapter.getId());
                infoVo.setLastChapterUpdateTime(LastChapter.getUpdateTime());
            }
            resList.add(infoVo);
        });
        Page<NovelSearchDetailInfoVo> resPage = new Page<>();
        BeanUtils.copyProperties(page, resPage, "records");
        resPage.setRecords(resList);
        return Result.OK(resPage);
    }
}
